#include "sensor_proc.h"
#include "kxtj3_driver.hpp"
#include "disp_proc.h"
// #include "ahtxx_driver.hpp"

#include "esp_log.h"
#include <cstring>

static const char *TAG = "sensor_proc";

KXTJ3Driver kxtj3_driver;
// AHT20Driver aht20_driver;
i2c_master_bus_handle_t bus_handle;

void i2c_interface_init(void)
{
    i2c_master_bus_config_t i2c_bus_config;
    memset(&i2c_bus_config, 0, sizeof(i2c_bus_config));
    i2c_bus_config.i2c_port = I2C_NUM_0;
    i2c_bus_config.sda_io_num = GPIO_NUM_4;
    i2c_bus_config.scl_io_num = GPIO_NUM_5;
    i2c_bus_config.clk_source = I2C_CLK_SRC_DEFAULT;
    i2c_bus_config.glitch_ignore_cnt = 7;
    i2c_bus_config.flags.enable_internal_pullup = 1;

    ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &bus_handle));
}

void seronr_kxtj3_init(void)
{
    // Initialize the KXTJ3 sensor
    KXTJ3Driver::KXJT3DriverConfig config;
    memset(&config, 0, sizeof(KXTJ3Driver::KXJT3DriverConfig));

    config.busHandle = bus_handle;
    config.i2cConfig.dev_addr_length = I2C_ADDR_BIT_LEN_7;
    config.i2cConfig.device_address = KXTJ3_I2C_ADDR;
    config.i2cConfig.scl_speed_hz = 100000;
    config.i2cConfig.scl_wait_us = 1000;
    config.addrWordlen = 1;
    config.writeTimeMs = 10;

    kxtj3_driver.i2cInterfaceInit(&config);
    kxtj3_driver.initialize();
    //
    int16_t xAccel, yAccel, zAccel; 
    kxtj3_driver.readAcceleration(xAccel, yAccel, zAccel);
    ESP_LOGI(TAG, "xAccel: %d, yAccel: %d, zAccel: %d", xAccel, yAccel, zAccel);
}

void seronr_kxtj3_test(void)
{
    // char buffer[32] = {0};
    int16_t xAccel, yAccel, zAccel;
    kxtj3_driver.readAcceleration(xAccel, yAccel, zAccel);
    ESP_LOGI(TAG, "xAccel: %d, yAccel: %d, zAccel: %d", xAccel, yAccel, zAccel);
    // int size = sprintf(buffer, "xAccel: %d", xAccel);
    // ssd1306.displayText(0, buffer, size, 0);
    // size = sprintf(buffer, "yAccel: %d", yAccel);
    // ssd1306.displayText(2, buffer, size, 0);
    // size = sprintf(buffer, "zAccel: %d", zAccel);
    // ssd1306.displayText(4, buffer, size, 0);
}

// void seronr_aht20_init(void)
// {
//     // Initialize the AHT20 sensor
//     AHT20Driver::AHT20DriverConfig config;
//     memset(&config, 0, sizeof(AHT20Driver::AHT20DriverConfig));

//     config.busHandle = bus_handle;
//     config.i2cConfig.dev_addr_length = I2C_ADDR_BIT_LEN_7;
//     config.i2cConfig.device_address = AHT20_I2C_ADDR;
//     config.i2cConfig.scl_speed_hz = 100000;
//     config.i2cConfig.scl_wait_us = 1000;
//     config.addrWordlen = 1;
//     config.writeTimeMs = 10;
    
//     if (aht20_driver.initialize(&config)) {
//         float temperature, humidity;
//         if (aht20_driver.readTemperatureAndHumidity(temperature, humidity)) {
//             ESP_LOGI(TAG, "Temperature: %.2f, Humidity: %.2f", temperature, humidity);
//         } else {
//             ESP_LOGE(TAG, "Failed to read temperature and humidity");
//         }
//     } else {
//         ESP_LOGE(TAG, "Failed to initialize AHT20");
//     }
// }

// void seronr_aht20_test(void)
// {
//     float temperature, humidity;
//     if (aht20_driver.readTemperatureAndHumidity(temperature, humidity)) {
//         ESP_LOGI(TAG, "Temperature: %.2f, Humidity: %.2f", temperature, humidity);
//     } else {
//         ESP_LOGE(TAG, "Failed to read temperature and humidity");
//     }
// }

